<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\commands;
use app\components\Func;
use app\models\AuthAccount;
use app\models\AuthItem;
use Yii;
use yii\console\Controller;
set_time_limit(0);
class AuthAccountController extends Controller
{
    //同步授权账户的视频数据
    public function actionSyncStatUserItem($auth_account_id){
        $obj = AuthAccount::findOne($auth_account_id);
        $obj->syncStatUserItem();
    }
    //更新用户所有账号基本信息
    public function actionUpdateAllBaseInfo($uid){
        $authAccounts = AuthAccount::findAll(['uid'=>$uid,'state'=>AuthAccount::STATE_GOOD]);
        foreach($authAccounts as $authAccount){
            $authAccount->updateAuthAccountInfo();
        }
    }
    //同步授权账户的关注和粉丝
    public function actionSyncFollowFans($auth_account_id){
        $obj = AuthAccount::findOne($auth_account_id);
        $obj->syncUserFollows();
        $obj->syncUserFans();
    }
    //同步授权账户发布项目
    public function actionSyncItems($auth_account_id){
        $obj = AuthAccount::findOne($auth_account_id);
        $obj->syncItems();
    }
    //同步所有授权账户发布项目-按时间区间
    public function actionSyncAllAuthAccountItemsByTime($uid,$begin_time,$end_time){
        $auth_account_ids = AuthAccount::find()->select('id')->where(['uid'=>$uid,'state'=>AuthAccount::STATE_GOOD])->column();
        $query = AuthItem::find()->where(['auth_account_id'=>$auth_account_ids]);
        $query->andFilterWhere(['>=','post_time', $begin_time]);
        $query->andFilterWhere(['<=','post_time', $end_time]);
        $query->andWhere(['state'=>1]);
        $authItems = $query->all();
        foreach($authItems as $authItem){
            $authItem->syncItem();
        }
    }
    //同步所有授权账户发布项目
    public function actionSyncAllAuthAccountItems($uid){
        $objs = AuthAccount::findAll(['uid'=>$uid,'state'=>AuthAccount::STATE_GOOD]);
        foreach($objs as $obj){
            $obj->syncItems();
//            Func::exec('auth-account/sync-items',[$obj->id],true);
        }
    }
    //同步授权账户最近发布的项目数据
    public function actionSyncLastItems($auth_account_id,$last_days = 1){
        $obj = AuthAccount::findOne($auth_account_id);
        $obj->syncItems();
    }
    //同步授权账户粉丝列表
    public function actionSyncUserFans($auth_account_id){
        $obj = AuthAccount::findOne($auth_account_id);
        $obj->syncUserFans();
    }
    //同步授权账户关注列表
    public function actionSyncUserFollows($auth_account_id){
        $obj = AuthAccount::findOne($auth_account_id);
        $obj->syncUserFollows();
    }

    //刷新某账号token
    public function actionRefreshToken($auth_account_id){
        $authAccount = AuthAccount::findOne($auth_account_id);
        if(empty($authAccount)){
            exit('account is null');
        }
        $authAccount->refreshToken();
    }
    //刷新某账号refresh_token
    public function actionRefreshRefreshToken($auth_account_id){
        $authAccount = AuthAccount::findOne($auth_account_id);
        if(empty($authAccount)){
            exit('account is null');
        }
        $authAccount->refreshRefreshToken();
    }
    //定时刷新快过期的token
    public function actionRefreshTokens(){
        AuthAccount::refreshTokens(3600);
    }
    //定时刷新快过期的refresh_token
    public function actionRefreshRefreshTokens(){
        AuthAccount::refreshRefreshTokens(3600);
    }
}
